<?php

function escapeRecursive($array) {
    $res = array();
    foreach ($array as $key => $value) {
        if (gettype($value) == "array") {
            $key = trim(htmlspecialchars(mysql_real_escape_string($key)));
            $res[$key] = escapeRecursive($value);
        } else {
            $key = trim(htmlspecialchars(mysql_real_escape_string($key)));
            $value = trim(htmlspecialchars(mysql_real_escape_string($value)));
            $res[$key] = $value;
        }
    }

    return $res;
}

function stripRecursive($array) {
    $res = array();
    foreach ($array as $key => $value) {
        if (gettype($value) == "array") {
            $key = stripslashes($key);
            $res[$key] = stripRecursive($value);
        } else {
            $key = htmlspecialchars_decode(stripslashes($key));
            $value = htmlspecialchars_decode(stripslashes($value));
            $res[$key] = $value;
        }
    }

    return $res;
}

function unsetAll(&$array) {
    foreach ($array as $key => $value) {
        unset($array[$key]);
    }
}

function xMail($site, $name, $from, $to, $subject, $message, $files, $types, $ids) {
    $boundary = $site . "-" . rand();
    $header = "";
    $header .= "From: \"" . $name . "\" <" . $from . ">\n";
    $header .= "Mime-Version: 1.0\n";
    $header .= "X-Mailer: " . $site . "\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"";

    $body = "";
    $body .= "--" . $boundary . "\n";
    $body .= "Content-type: text/html; charset=\"utf-8\"\n";
    $body .= "Content-Transfer-Encoding: 8bit\n\n";
    $body .= $message . "\n\n";
    foreach ($files as $fileName => $fileContent) {
        $body .= "--" . $boundary . "\n";
        $body .= "Content-Type: " . $types[$fileName] . "; name=\"" . $fileName . "\"\r\n";
        $body .= "Content-Transfer-Encoding: base64\n";
        $body .= "Content-ID: <" . $ids[$fileName] . ">\n";
        $body .= "Content-Disposition: attachment\n\n";
        $body .= base64_encode($fileContent) . "\n\n";
    }

    return mail($to, $subject, $body, $header);
}

function BBCodeToHtml($sm, $text) {

    $text = trim(htmlspecialchars(stripslashes($text)));

    define(EMOTICONS_DIR, $sm->template_dir . "images/emoticons/");

    $bbSmilesRegular = array();
    $bbSmilesPattern = array();

    $bbSmilesRegular[] = ":)";
    $bbSmilesPattern[] = "<img alt=\":)\" src=\"" . EMOTICONS_DIR . "emoticon-happy.png\" />";

    $bbSmilesRegular[] = ":D";
    $bbSmilesPattern[] = "<img alt=\":D\" src=\"" . EMOTICONS_DIR . "emoticon-smile.png\" />";

    $bbSmilesRegular[] = ":o";
    $bbSmilesPattern[] = "<img alt=\":o\" src=\"" . EMOTICONS_DIR . "emoticon-surprised.png\" />";

    $bbSmilesRegular[] = ":p";
    $bbSmilesPattern[] = "<img alt=\":p\" src=\"" . EMOTICONS_DIR . "emoticon-tongue.png\" />";

    $bbSmilesRegular[] = ":(";
    $bbSmilesPattern[] = "<img alt=\":(\" src=\"" . EMOTICONS_DIR . "emoticon-unhappy.png\" />";

    $bbSmilesRegular[] = ";)";
    $bbSmilesPattern[] = "<img alt=\";)\" src=\"" . EMOTICONS_DIR . "emoticon-wink.png\" />";

    $text = str_replace($bbSmilesRegular, $bbSmilesPattern, $text);

    //---------------------------------------------------------------------------------------------

    function parseBTagRecursive($text) {
        $regex = "'
                        \[b\]
                            (.*)
                        \[/b\]
                   'iusx";

        if (is_array($text)) {
            $text = "<strong>" . $text[1] . "</strong>";
        }

        return preg_replace_callback($regex, "parseBTagRecursive", $text);
    }

    $text = parseBTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseITagRecursive($text) {
        $regex = "'
                        \[i\]
                            (.*)
                        \[/i\]
                   'iusx";

        if (is_array($text)) {
            $text = "<em>" . $text[1] . "</em>";
        }

        return preg_replace_callback($regex, "parseITagRecursive", $text);
    }

    $text = parseITagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseUTagRecursive($text) {
        $regex = "'
                        \[u\]
                            (.*)
                        \[/u\]
                   'iusx";

        if (is_array($text)) {
            $text = "<span style=\"text-decoration: underline;\">" . $text[1] . "</span>";
        }

        return preg_replace_callback($regex, "parseUTagRecursive", $text);
    }

    $text = parseUTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseCodeTagRecursive($text) {
        $regex = "'
                        \[code\]
                            (.*)
                        \[/code\]
                   'iusx";

        if (is_array($text)) {
            $text = "<code style=\"white-space: pre;\">" . $text[1] . "</code>";
        }

        return preg_replace_callback($regex, "parseCodeTagRecursive", $text);
    }

    $text = parseCodeTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseEmailTagRecursive($text) {
        $regex = "'
                        \[email\]
                            (.*)
                        \[/email\]
                   'iusx";

        if (is_array($text)) {
            $text = "<a href=\"mailto:" . $text[1] . "\">" . $text[1] . "</a>";
        }

        return preg_replace_callback($regex, "parseEmailTagRecursive", $text);
    }

    $text = parseEmailTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseUrlTagRecursive($text) {

        $regex = "'
                        \[url=\"?([^\[]*)\"?\]
                            (.*)
                        \[/url\]
                   'iusx";

        if (is_array($text)) {
            $text = "<a href=\"" . $text[1] . "\">" . $text[2] . "</a>";
        }

        return preg_replace_callback($regex, "parseUrlTagRecursive", $text);
    }

    $text = parseUrlTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseSizeTagRecursive($text) {
        $regex = "'
                        \[size=\"?([^\[]*)\"?\]
                            (.*)
                        \[/size\]
                   'iusx";

        if (is_array($text)) {
            $text = "<span style=\"font-size: " . $text[1] . "px;\">" . $text[2] . "</span>";
        }

        return preg_replace_callback($regex, "parseSizeTagRecursive", $text);
    }

    $text = parseSizeTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseColorTagRecursive($text) {
        $regex = "'
                        \[color=\"?([^\]]*)\"?\]
                            (.*)
                        \[/color\]
                   'iusx";

        if (is_array($text)) {
            $text = "<span style=\"color: " . $text[1] . ";\">" . $text[2] . "</span>";
        }

        return preg_replace_callback($regex, "parseColorTagRecursive", $text);
    }

    $text = parseColorTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseQuoteTagRecursive($text) {
        $regex = "'
                        \[quote\]
                            (.*)
                        \[/quote\]
                   'iusx";

        if (is_array($text)) {
            $text = "<blockquote>" . $text[1] . "</blockquote>";
        }

        return preg_replace_callback($regex, "parseQuoteTagRecursive", $text);
    }

    $text = parseQuoteTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseLiTagRecursive($text) {
        $regex = "'
                        \[\*\]
                            ([^\[]*)
                        \n
                   'iusx";

        if (is_array($text)) {
            $text = "<li>" . $text[1] . "</li>";
        }

        return preg_replace_callback($regex, "parseLiTagRecursive", $text);
    }

    $text = parseLiTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseListNumTagRecursive($text) {
        $regex = "'
                        \[list=\"?([^\]]*)\"?\]
                            (.*)
                        \[/list\]
                   'iusx";

        if (is_array($text)) {
            $text = "<ol start=\"" . $text[1] . ";\">" . $text[2] . "</ol>";
        }

        return preg_replace_callback($regex, "parseListNumTagRecursive", $text);
    }

    $text = parseListNumTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseListTagRecursive($text) {
        $regex = "'
                        \[list\]
                            (.*)
                        \[/list\]
                   'iusx";

        if (is_array($text)) {
            $text = "<ul>" . $text[1] . "</ul>";
        }

        return preg_replace_callback($regex, "parseListTagRecursive", $text);
    }

    $text = parseListTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    function parseImgTagRecursive($text) {
        $regex = "'
                        \[img=\"?([^\]]*)\"?\]
                            (.*)
                        \[/img\]
                   'iusx";

        if (is_array($text)) {
            $text = "<img src=\"" . $text[2] . "\" alt=\"\" />";
        }

        return preg_replace_callback($regex, "parseImgTagRecursive", $text);
    }

    $text = parseImgTagRecursive($text);

    //---------------------------------------------------------------------------------------------

    return str_replace("\n", "<br />", $text);
}

function bbCodeClear($text) {
    $bbCodeRegular = array();
    $bbCodeCleanValue = array();

    $bbCodeRegular[] = "'\[b\][\s]*(.*)[\s]*\[/b\]'iu";
    $bbCodeCleanValue[] = "\$1";

    $bbCodeRegular[] = "'\[i\][\s]*(.*)[\s]*\[/i\]'iu";
    $bbCodeCleanValue[] = "\$1";

    $bbCodeRegular[] = "'\[u\][\s]*(.*)[\s]*\[/u\]'iu";
    $bbCodeCleanValue[] = "\$1";

    $bbCodeRegular[] = "'\[code\][\s]*(.*)[\s]*\[/code\]'iu";
    $bbCodeCleanValue[] = "\$1";

    $bbCodeRegular[] = "'\[img=\"?([^\]]*)\"?\][\s]*(.*)[\s]*\[/img\]'iu";
    $bbCodeCleanValue[] = "";

    $bbCodeRegular[] = "'\[email\][\s]*(.*)[\s]*\[/email\]'iu";
    $bbCodeCleanValue[] = "\$1";

    $bbCodeRegular[] = "'\[url=\"?([^\]]*)\"?\][\s]*(.*)[\s]*\[/url\]'iu";
    $bbCodeCleanValue[] = "\$2";

    $bbCodeRegular[] = "'\[size=\"?([^\]]*)\"?\][\s]*(.*)[\s]*\[/size\]'iu";
    $bbCodeCleanValue[] = "\$2";

    $bbCodeRegular[] = "'\[color=\"?([^\]]*)\"?\][\s]*(.*)[\s]*\[/color\]'iu";
    $bbCodeCleanValue[] = "\$2";

    $bbCodeRegular[] = "'\[quote\][\s]*(.*)[\s]*\[/quote\]'iu";
    $bbCodeCleanValue[] = "\$1";

    $bbCodeRegular[] = "'\[\*\]([^\[]*)\n'iu";
    $bbCodeCleanValue[] = "\$1";

    $bbCodeRegular[] = "'\[list=\"?([^\]]*)\"?\][\s]*(.*)[\s]*\[/list\]'iu";
    $bbCodeCleanValue[] = "\$2";

    $bbCodeRegular[] = "'\[list\][\s]*(.*)[\s]*\[/list\]'iu";
    $bbCodeCleanValue[] = "\$1";

    return preg_replace($bbCodeRegular, $bbCodeCleanValue, $text);
}

function getNextImagePath($curRealDir, $original) {
    $imagesDirs = scandir($curRealDir . "/images");

    $createNewFolder = true;
    $folder = "";
    foreach ($imagesDirs as $key => $value) {
        if ($value != ".." && $value != ".") {
            $curImages = scandir($curRealDir . "/images/" . $value);
            if (count($curImages) < 102) {
                $createNewFolder = false;
                $folder = $value;
            }
        }
    }
    if ($createNewFolder) {
        mkdir($curRealDir . "/images/" . (count($imagesDirs) - 1), 777);
        $folder = count($imagesDirs) - 1;
    }

    $filename = md5(mb_substr($original, 0, mb_strpos($original, ".")));
    $extention = mb_substr($original, mb_strpos($original, ".") + 1);

    while (file_exists($curRealDir . "/images/" . $folder . "/" . $filename . "." . $extention)) {
        $filename = md5($filename . rand());
    }

    return "/images/" . $folder . "/" . $filename . "." . $extention;
}

function getOption($db, $option) {
    $db->dbq("
                Select
                    ".$option."
                From
                    options
            ");
    $arr = $db->row();
    return $arr[0];
}